<template>
  <div class="recycle-page">
    <div class="recycle-header">
      <div class="header-left">
        <button class="back-btn" @click="goToHome">
          <SvgIcon name="back" width="16" height="16" />
        </button>
        <div class="title-group">
          <h1>回收站</h1>
          <span class="subtitle">管理已删除的待办事项</span>
        </div>
      </div>
    </div>
    <div class="recycle-content">
      <div v-if="loading" class="loading-state">
        <div class="loading-spinner"></div>
        <p>加载中...</p>
      </div>
      <div v-else-if="!hasItems" class="empty-state">
        <SvgIcon name="trash" width="64" height="64" />
        <p>回收站是空的</p>
        <span class="empty-desc">删除的待办事项将会显示在这里</span>
      </div>
      <div v-else class="items-list">
        <!-- 自定义待办项布局 -->
        <div v-for="item in deletedItems" :key="item.todoItemId" class="recycle-item">
          <div class="item-content">
            <div class="item-text">{{ item.content }}</div>
            <div class="item-info">
              <span class="item-date">删除时间: {{ formatDate(item.updatedAt) }}</span>
            </div>
          </div>
          <div class="item-actions">
            <button
              class="action-btn restore-btn"
              @click="restoreItem(item)"
              :disabled="restoring === item.todoItemId"
              title="恢复"
            >
              <SvgIcon name="restore" width="16" height="16" />
              <span>恢复</span>
            </button>
            <button
              class="action-btn delete-btn"
              @click="deleteItem(item)"
              :disabled="deleting === item.todoItemId"
              title="彻底删除"
            >
              <SvgIcon name="delete" width="16" height="16" />
              <span>彻底删除</span>
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import SvgIcon from '@/components/SvgIcon.vue'
import { useRouter } from 'vue-router'
import { getDeletedTodos, restoreDeletedTodo } from '@/api/todo'

const router = useRouter()
const deletedItems = ref([])
const loading = ref(true)
const restoring = ref(null) // 正在恢复的待办ID
const deleting = ref(null) // 正在删除的待办ID

const hasItems = computed(() => deletedItems.value.length > 0)

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''

  const date = new Date(dateString)
  const now = new Date()
  const isToday = date.toDateString() === now.toDateString()

  if (isToday) {
    return `今天 ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
  }

  return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
}

// 获取回收站中的已删除待办事项
const fetchDeletedTodos = async () => {
  loading.value = true
  try {
    const result = await getDeletedTodos()
    if (result.success) {
      deletedItems.value = result.data
    } else {
      console.error('获取已删除待办事项失败:', result.msg)
    }
  } catch (error) {
    console.error('获取已删除待办事项出错:', error)
  } finally {
    loading.value = false
  }
}

onMounted(async () => {
  await fetchDeletedTodos()
})

const goToHome = () => {
  router.push('/')
}

// 实现恢复待办项功能
const restoreItem = async (item) => {
  try {
    // 设置恢复中状态，防止重复操作
    restoring.value = item.todoItemId

    // 调用恢复API
    const result = await restoreDeletedTodo({ id: item.todoItemId })

    if (result.success) {
      // 从列表中移除已恢复的待办
      deletedItems.value = deletedItems.value.filter((todo) => todo.todoItemId !== item.todoItemId)
    } else {
      console.error('恢复待办事项失败:', result.msg)
    }
  } catch (error) {
    console.error('恢复待办事项出错:', error)
  } finally {
    // 清除恢复中状态
    restoring.value = null
  }
}

const deleteItem = (item) => {
  // TODO: 实现删除单个待办事项的逻辑
  deleting.value = item.todoItemId

  // 模拟删除操作，实际实现需要连接API
  setTimeout(() => {
    console.log('彻底删除待办事项:', item)
    // 从列表中移除
    deletedItems.value = deletedItems.value.filter((todo) => todo.todoItemId !== item.todoItemId)
    deleting.value = null
  }, 500)
}
</script>

<style scoped>
.recycle-page {
  padding: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.recycle-header {
  padding: 20px 24px;
  border-bottom: 1px solid #e5e5ea;
  background: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.title-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.header-left h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  color: #1d1d1f;
}

.subtitle {
  font-size: 13px;
  color: #86868b;
}

.recycle-content {
  flex: 1;
  padding: 24px;
  background-color: #ffffff;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.empty-state {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #86868b;
  gap: 12px;
}

.empty-state p {
  margin: 0;
  font-size: 15px;
  color: #1d1d1f;
}

.empty-desc {
  font-size: 13px;
}

.items-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 0 16px;
  overflow-y: auto;
  height: 100%;
  padding-right: 8px;
}

/* 回收站待办项样式 */
.recycle-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background-color: #f9f9f9;
  border-radius: 12px;
  cursor: default;
  transition: all 0.2s;
  position: relative;
  overflow: hidden;
}

.recycle-item:hover {
  background-color: #f5f5f7;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.item-content {
  flex: 1;
  min-width: 0;
  margin-right: 16px;
}

.item-text {
  font-size: 15px;
  color: #1d1d1f;
  word-break: break-word;
  line-height: 1.4;
  text-decoration: line-through;
  color: #86868b;
  margin-bottom: 6px;
}

.item-info {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

.item-date {
  font-size: 12px;
  color: #86868b;
}

.item-actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 6px;
  border: none;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.restore-btn {
  background-color: #4361ee;
  color: white;
}

.restore-btn:hover:not(:disabled) {
  background-color: #3a56d4;
}

.delete-btn {
  background-color: #ff3b30;
  color: white;
}

.delete-btn:hover:not(:disabled) {
  background-color: #ff2d55;
}

.back-btn {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  border: none;
  background-color: #f5f5f7;
  color: #1d1d1f;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
}

.back-btn:hover {
  background-color: #e5e5ea;
}

/* 加载状态样式 */
.loading-state {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #86868b;
  gap: 12px;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(67, 97, 238, 0.2);
  border-radius: 50%;
  border-top-color: #4361ee;
  animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* 自定义滚动条样式 */
.items-list::-webkit-scrollbar {
  width: 6px;
}

.items-list::-webkit-scrollbar-track {
  background: transparent;
}

.items-list::-webkit-scrollbar-thumb {
  background-color: #d2d2d7;
  border-radius: 3px;
}

.items-list::-webkit-scrollbar-thumb:hover {
  background-color: #86868b;
}
</style>
